package com.lucy.property.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.property.entity.HouseShip;
import com.lucy.property.entity.ParkingLot;
import com.lucy.property.entity.PropertyUnit;
import com.lucy.property.params.ParkingLotCreateParams;
import com.lucy.property.params.ParkingLotQueryParams;
import com.lucy.property.params.ParkingLotUpdateParams;
import com.lucy.property.service.ParkingLotService;
import com.lucy.property.service.PropertyUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@Api(tags = "停车场管理")
public class ParkingLotController {
    @Autowired
    ParkingLotService service;

    @Autowired
    PropertyUnitService unitService;

    @PostMapping("parkinglot")
    @ApiOperation(value = "停车场新建")
    public ResponseDTO<Object> parkingLotCreate(@RequestBody @Validated ParkingLotCreateParams params){
        ParkingLot parkingLot = new ParkingLot();
        // 获取单元信息
        // 获取所在单元信息
        Optional<PropertyUnit> unitOp = unitService.getOne(params.getUnitId());
        if (!unitOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND,"无对应单元信息");
        }
        PropertyUnit unit = unitOp.get();
        parkingLot.setUnitCode(unit.getUnitCode());
        parkingLot.setUnitName(unit.getUnitName());

        BeanUtil.copyProperties(params,parkingLot, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return ResponseDTO.succData(
                service.saveOne(parkingLot)
        );
    }

    @DeleteMapping("parkinglot/{id}")
    @ApiOperation(value = "停车场删除",notes = "")
    public ResponseDTO<Object> parkingLotDelete(@PathVariable("id") String id){
        service.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("parkinglot")
    @ApiOperation(value = "停车场更新",notes = "")
    public ResponseDTO<Object> parkingLotUpdate(@RequestBody @Validated ParkingLotUpdateParams params){
        String parkingId = params.getId();
        Optional<ParkingLot> parkingLotOp = service.getOne(parkingId);
        if (!parkingLotOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }

        ParkingLot parkingLot = parkingLotOp.get();
        // 获取所在单元信息
        Optional<PropertyUnit> unitOp = unitService.getOne(params.getUnitId());
        if (!unitOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND,"无对应单元信息");
        }
        PropertyUnit unit = unitOp.get();
        parkingLot.setUnitCode(unit.getUnitCode());
        parkingLot.setUnitName(unit.getUnitName());
        // 执行更新
        BeanUtil.copyProperties(params,parkingLot,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(
                service.updateOne(parkingLot)
        );
    }

    @GetMapping("parkinglot")
    @ApiOperation(value = "停车场列表",notes = "")
    public ResponseDTO<Object> parkingLotListQuery(ParkingLotQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        ParkingLot item = new ParkingLot();
        BeanUtil.copyProperties(params,item,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<ParkingLot> example = Example.of(item);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("parkinglot/{id}")
    @ApiOperation(value = "停车场查询")
    public ResponseDTO<Object> parkingLotQuery(@PathVariable("id") String id){
        Optional<ParkingLot> itemOp = service.getOne(id);
        return itemOp.<ResponseDTO<Object>>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(HttpStatus.NOT_FOUND));
    }

}
